import { accAdd, accMul, accDiv,fullLink,copy,isFunction } from "@/common/utils";
import { timeFormat } from "@/uni_modules/endless-ui/libs/utils/lodash/date";
import type { UniAppConfig,SelectDictLabel } from './type'
// #ifdef MP-WEIXIN
import {mpAppStaticPath} from '@/common/init/mpApp'
// #endif
// #ifdef MP-ALIPAY
import {mpAppStaticPath} from '@/common/init/aliApp'
// #endif


const selectDictLabel: SelectDictLabel = (datas, value) => {
	let actions: string[] = [];
	datas.some((item) => {
		
		if (item.dictValue === `${value}`) {
			actions.push(item.dictLabel);
			return true;
		}
	});
	return actions.join("");
};




export default {
	init({ globalProperties }: UniAppConfig) {
		globalProperties.$timeFormat = timeFormat;
		// 回显数据字典
		globalProperties.$selectDictLabel = selectDictLabel
		/**
		 * H5 复制
		 */
		globalProperties.$copy = function (content: string,backCall: (() => void) | boolean = true) {
			copy(content,() => {
				this.$toast(this.$t("common.复制成功"));
				if(isFunction(backCall)){
					backCall()
				}
			})
		};
		globalProperties.$accMul = accMul;

		globalProperties.$accAdd = accAdd;

		globalProperties.$accDiv = accDiv;
		globalProperties.$fullLink = function (url:string) {
			return fullLink(this.$pinia.state.value?.global?.global?.host,url)
		}
		globalProperties.$staticPath = function (url:string) {
			// #ifdef H5
			return `/static/h5/${url}`
			// #endif
			// #ifdef APP
			return `/static/app/${url}`
			// #endif
			// #ifdef MP-WEIXIN||MP-ALIPAY
			return mpAppStaticPath.value + url
			// #endif
		}
		globalProperties.$getShareUrlScene = (scene:string) => {
			// #ifdef SUBSCRIPTION || PRIVATE
			scene = `${scene ? (scene+'&') : '' }tenant=${uni.getStorageSync('tenant')}`
			// #endif
			return scene.trim()
		}
		// 数字格式化 1000=1k 100000 = 10k
		globalProperties.$formatNumberToK = (number:string|number) => {
			let num = `${number}`
			if (Number(number) >= 1000) {
				num = Math.floor(Number(number) / 1000).toLocaleString() + 'k'
			}
			return num
		}
		
		globalProperties.$pageLoad = (loadList = [], addList = [], pageNo = 1, pageSize = 10, cache = true) => {
			let loadStatus = false
			if (addList.length != 0 || !cache) {
				// 判断是否为第一次加载 or 第一页加载未满
				if (loadList.length == 0 || loadList.length < pageSize) {
					// 判断数据是否有变动
					if (loadList.length != addList.length || !cache) {
						loadList.splice(0, loadList.length)
						loadList.push(...addList)
						loadStatus = false
					}
				} else {
					let more =  loadList.length % pageSize
					// 目前加载的页数是否全部为满页
					if (more == 0) {
						loadList.push(...addList)
						loadStatus = true;
					} else {
						if (more < addList.length) {
							loadStatus = true
							// 加载剩余的数据
							for (let i = more; i < addList.length; i++) {
								loadList.push(addList[i])
							}
						}
					}
				}
				// return loadList;
				// 加载数据是否够一页
				if (addList.length == pageSize) {
					pageNo++
				}
				return {
					loadStatus: loadStatus,
					list: loadList,
					pageNo: pageNo,
				}
			} else {
				return {
					loadStatus: false,
					list: loadList,
					pageNo: pageNo,
				}
			}
		}
	},
};
